/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package util.log;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author Rdelacruz
 */
public class MiLoggerHtml extends MiLogger {
    
    //StringBuilder sb;
    HttpServletResponse response;
    PrintWriter writer;
    MiLogger loggerError;
    
    protected MiLoggerHtml(HttpServletResponse response, MiLogger loggerError) {
    //protected LoggerHtml() {
        
        try {
            this.loggerError = loggerError;
            this.response = response;
            this.writer = response.getWriter();
        } catch (IOException ex) {
            //Logger.getLogger(LogRequest.class.getName()).log(Level.SEVERE, null, ex);
            MiLogger.getLogger().loge(ex);
        }
                
        //sb = new StringBuilder();
    }
    
    //@Override
    //protected String getLog() {
    //    return sb.toString();
    //}
    
     
    @Override
    protected void mostrarLinea(String texto) {
        if(writer!=null) {            
            writer.append(texto+"<br/>");
        }
    }    
    
    @Override
    protected void mostrar(Exception e) {
        //Mostrando Resumen de la Excepcion
        mostrarLineaError(e.getMessage());
        Throwable tr = e.getCause();
        if(tr!=null) {
            String mensaje = "";
            String msj = tr.getMessage();
            if(msj==null) {
                //mensaje = mensaje.concat(e.getLocalizedMessage());
                //mensaje = mensaje.concat( e.getClass().getName() );
                mensaje = mensaje.concat( tr.getClass().getSimpleName() );     
                //System.out.println(e.getClass().getSimpleName());
            }
            else {   
                mensaje = mensaje.concat(msj);
                //System.out.println(msj);
            }
            mostrarLinea(mensaje);
        }
        
        if(loggerError!=null)
            loggerError.loge(e);
    }
    
}
